###### Replication codes for GINI-2015-0764 "Improving Reputation BIT by BIT: Bilateral Investment Treaties and Foreign Accountability" by Chia-yi Lee & Noel Johnston ######

## Monadic analysis ##
Mdata <- read.csv("GINI-2015-0764.R1_MonadicData.csv") #read the monadic data 

# Table 1 #
m1 <- lm(lnFDI~lnFDI1+bitcount+lnoecd+lnGDP.1+lnGDPpc.1+growth.1+trade.1+polity2.x+offxrate_lcudif+ptacount+lnaid+lnpop+factor(country)+factor(year), subset(Mdata, oecd==0 &country!="China"))  #Model 1
summary(m1)
m2 <- lm(lnFDI~lnFDI1+bit_power+bit_other+lnoecd+lnGDP.1+lnGDPpc.1+growth.1+trade.1+polity2.x+offxrate_lcudif+ptacount+lnaid+lnpop+factor(country)+factor(year), subset(data3, oecd==0 &country!="China"))  #Model 2
summary(m2)
m3 <- lm(lnFDI~lnFDI1+bit_power+lnoecd+lnGDP.1+lnGDPpc.1+growth.1+trade.1+polity2.x+offxrate_lcudif+ptacount+lnaid+lnpop+factor(country)+factor(year), subset(data3, oecd==0 &country!="China"))  #Model 3
summary(m3)
m4 <- lm(lnFDI~lnFDI1+bit_other+lnoecd+lnGDP.1+lnGDPpc.1+growth.1+trade.1+polity2.x+offxrate_lcudif+ptacount+lnaid+lnpop+factor(country)+factor(year), subset(data3, oecd==0 &country!="China"))  #Model 4
summary(m4)

# Figure 1 #
k <- subset(Mdata, year==2006 & oecd==0 &country!="China")
par(mfrow=c(1, 2), mar=c(4,4,2,2))
barplot(table(k$bit_power), cex.axis=1.2)
mtext(side=1, "Number of powerful BITs signed", line=2.5, cex=1.3)
mtext(side=2, "Frequency", line=2.5, cex=1.5)
barplot(table(k$bit_other), ylim=c(0, 32), cex.axis=1.2)
mtext(side=1, "Number of other BITs signed", line=2.5, cex=1.3)

# Figure 2 #
time.x <- c(0:10);

beta <- coef(m2)[3];
phi <- coef(m2)[2];

time1 <- beta;
time2 <- beta*(1+phi);
time3 <- beta*(1+phi+phi^2);
time4 <- beta*(1+phi+phi^2+phi^3);
time5 <- beta*(1+phi+phi^2+phi^3+phi^4);
time6 <- beta*(1+phi+phi^2+phi^3+phi^4+phi^5);
time7 <- beta*(1+phi+phi^2+phi^3+phi^4+phi^5+phi^6);
time8 <- beta*(1+phi+phi^2+phi^3+phi^4+phi^5+phi^6+phi^7);
time9 <- beta*(1+phi+phi^2+phi^3+phi^4+phi^5+phi^6+phi^7+phi^8+phi^9);
time10 <- beta*(1+phi+phi^2+phi^3+phi^4+phi^5+phi^6+phi^7+phi^8+phi^9+phi^10);

change.y <- c(0,time1,time2,time3,time4,time5,time6,time7,time8,time9, time10);
names(change.y) <- time.x;

par(mfrow=c(1,1),mar=c(3,4,1,1),oma=c(2,3,1,1));
barplot(change.y, space=.7, ylab="", xlab="", axes=F, ylim=c(0,0.20), cex.names=1.4, cex=1.6, cex.lab=1.4);
axis(side=2, tick=TRUE, cex.axis=1.3, las=1);
mtext(side=1, "Time points (year)", cex=1.3, line=3)
mtext(side=2, "Change in logged FDI (million) with one powerful BIT", cex=1.3, line=4)
box()

# Web Appendix - matching #
library(Matching)
data4 <- as.data.frame(Mdata)
data4 <- cbind(data4["lnFDI"], data4["lnFDI1"], data4["bit_power"], data4["bit_other"], data4["bitcount"], data4["lnoecd"], data4["lnGDP.1"], data4["lnGDPpc.1"], data4["growth.1"], data4["trade.1"], data4["polity2.x"], data4["offxrate_lcudif"], data4["ptacount"], data4["lnaid"], data4["lnpop"], data4["country"], data4["year"])
data4 <- data4[complete.cases(data4),]
data4 <- subset(data4, country!="China")
t <- ifelse(data4$bit_power>0, 1, data4$bit_power)
glm <- glm(t~lnoecd+lnGDP.1+lnGDPpc.1+growth.1+trade.1+polity2.x+offxrate_lcudif+ptacount+lnaid+lnpop, data=data4, family=binomial)
rr <- Match(Y=data4$lnFDI, Tr=t, X=cbind(glm$fitted, data4["lnFDI1"], data4["bit_other"]), M=1)
summary(rr) # the treatment effect #

# Web Appendix - Table 3 #
m1 <- lm(lnoecd~lnoecd1+bitcount+lnother1+lnGDP.1+lnGDPpc.1+growth.1+trade.1+polity2.x+proprights+I(offxrate_lcudif/100)+ptacount+lnpop+factor(country)+factor(year), subset(Mdata, country!="China"))
summary(m1)
m2 <- lm(lnoecd~lnoecd1+bit_power+bit_other+lnother1+lnGDP.1+lnGDPpc.1+growth.1+trade.1+polity2.x+proprights+I(offxrate_lcudif/100)+ptacount+lnpop+factor(country)+factor(year), subset(Mdata, country!="China"))
summary(m2)
m3 <- lm(lnoecd~lnoecd1+bit_power+lnother1+lnGDP.1+lnGDPpc.1+growth.1+trade.1+polity2.x+proprights+I(offxrate_lcudif/100)+ptacount+lnpop+factor(country)+factor(year), subset(Mdata, country!="China"))
summary(m3)
m4 <- lm(lnoecd~lnoecd1+bit_other+lnother1+lnGDP.1+lnGDPpc.1+growth.1+trade.1+polity2.x+proprights+I(offxrate_lcudif/100)+ptacount+lnpop+factor(country)+factor(year), subset(Mdata, country!="China"))
# FDI from other countries #
summary(m4)
m5 <- lm(lnother~lnother1+bitcount+lnoecd1+lnGDP.1+lnGDPpc.1+growth.1+trade.1+polity2.x+proprights+offxrate_lcudif+ptacount+lnpop+factor(country)+factor(year), subset(Mdata, country!="China"))
summary(m5)
m6 <- lm(lnother~lnother1+bit_power+bit_other+lnoecd1+lnGDP.1+lnGDPpc.1+growth.1+trade.1+polity2.x+proprights+offxrate_lcudif+ptacount+lnpop+factor(country)+factor(year), subset(Mdata, country!="China"))
summary(m6)
m7 <- lm(lnother~lnother1+bit_power+lnoecd1+lnGDP.1+lnGDPpc.1+growth.1+trade.1+polity2.x+proprights+offxrate_lcudif+ptacount+lnpop+factor(country)+factor(year), subset(Mdata, country!="China"))
summary(m7)
m8 <- lm(lnother~lnother1+bit_other+lnoecd1+lnGDP.1+lnGDPpc.1+growth.1+trade.1+polity2.x+proprights+offxrate_lcudif+ptacount+lnpop+factor(country)+factor(year), subset(Mdata, country!="China"))
summary(m8)

## Dyadic analysis ##
Ddata <- read.csv("GINI-2015-0764.R1_DyadicData.csv") #read the dyadic data

# Table 2 #
m1 <- lm(lndyadfdi2~lndyadfdi21+dyad_bit+I(bit_power-dyad_bit)+bit_other+factor(dyadid)+factor(year), subset(Ddata, country!="China"))
summary(m1)
m2 <- lm(lndyadfdi2~lndyadfdi21+dyad_bit+I(bit_power-dyad_bit)+bit_other+llnGDPratio_g+lnGDP.1+lnGDPpc.1+growth.1+trade.1+polity2.x+offxrate_lcudif+ptacount+lnaid+lnpop+factor(dyadid)+factor(year), subset(Ddata, country!="China"))
summary(m2)
m3 <- lm(lndyadfdi2~lndyadfdi21+dyad_bit+llnGDPratio_g+lnGDP.1+lnGDPpc.1+growth.1+trade.1+polity2.x+offxrate_lcudif+ptacount+lnaid+lnpop+factor(dyadid)+factor(year), subset(dyad2, country!="China"))
summary(m3)
m4 <- lm(lndyadfdi2~lndyadfdi21+dyad_bit+I(bit_power-dyad_bit)+llnGDPratio_g+lnGDP.1+lnGDPpc.1+growth.1+trade.1+polity2.x+offxrate_lcudif+ptacount+lnaid+lnpop+factor(dyadid)+factor(year), subset(dyad2, country!="China"))
summary(m4)
m5 <- lm(lndyadfdi2~lndyadfdi21+dyad_bit+bit_other+llnGDPratio_g+lnGDP.1+lnGDPpc.1+growth.1+trade.1+polity2.x+offxrate_lcudif+ptacount+lnaid+lnpop+factor(dyadid)+factor(year), subset(dyad2, country!="China"))
summary(m5)

#Note: dyad$dyad_fdi2 <- dyad$oecd_fdi-dyad$dyad_fdi; dyad$lndyadfdi2 <- ifelse(dyad$dyad_fdi2>0, log(dyad$dyad_fdi2*1000000/dyad$cpi), 0)
